package com.jbh.fourclibrary.fragment;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jbh.fourclibrary.R;
import com.jbh.fourclibrary.activity.PlayMp4Activity;
import com.jbh.fourclibrary.util.Util;
import com.jbh.fourclibrary.view.MaterialVideoView;

import java.io.IOException;

public class ClarityFragment extends Fragment implements RadioGroup.OnCheckedChangeListener{

    public MaterialVideoView mSurfaceView;
    private RadioGroup gClarity;
    private TextView tv_name;
    private String[] s={
            "指在10倍宝石放大镜下观察钻石内部无任何瑕疵，表面也没有瑕疵，光彩照人。净度非常完美。",
            "指在10倍宝石放大镜下观察钻石可见到亭部或表面有非常微小的棉状点和小毛茬等。",
            "指在10倍宝石放大镜下观察钻石可能看到比较明显的瑕疵和小毛茬在。肉眼看不到钻石的瑕疵。",
            "指在10倍宝石放大镜下观察钻石很容易见到瑕疵，有些钻石瑕疵肉眼可以看到比较明显。",
    };

    public ClarityFragment() {
    }

    public static ClarityFragment newInstance() {
        ClarityFragment fragment = new ClarityFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_clarity, container, false);
        initView(view);
        measure(view);
        return view;
    }


    private void measure(View view) {
        View clarity_view = view.findViewById(R.id.clarity_view);
        ViewGroup.LayoutParams param = clarity_view.getLayoutParams();
        param.height = Util.getHeight(getActivity())
                - Util.getStatusBarHeight(getActivity())
                - Util.dpToPx(getActivity(),90);

        ViewGroup.LayoutParams p = mSurfaceView.getLayoutParams();
        p.height =  param.height * 5/7 ;
        p.width = param.height * 6/7 ;

        p=tv_name.getLayoutParams();
        tv_name.setPadding(0,20,0,20);
        p.width =  param.height * 6/7;

        p=gClarity.getLayoutParams();
        p.height =  param.height * 5/7 ;
    }





    /**
     *
     * @param view
     */
    private void initView(View view) {
        mSurfaceView=view.findViewById(R.id.mClaritySurfaceView);
        mSurfaceView.setSupportLoading(false);
        gClarity = view.findViewById(R.id.gClarity);
        gClarity.setOnCheckedChangeListener(this);
        tv_name=view.findViewById(R.id.tv_name);
        tv_name.setText(s[0]);
        view.findViewById(R.id.tv_play).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(), PlayMp4Activity.class).putExtra("av_name","av_clarity"));
            }
        });
        initPlayAv("av_clarity_fl");
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        String avName = null;
        if (checkedId == R.id.r_fl) {
            avName = "av_clarity_fl";
            tv_name.setText(s[0]);
        } else if (checkedId == R.id.r_si) {
            avName = "av_clarity_si";
            tv_name.setText(s[3]);
        } else if (checkedId == R.id.r_vs1) {
            avName = "av_clarity_vs";
            tv_name.setText(s[2]);
        } else if (checkedId == R.id.r_vvs1) {
            avName = "av_clarity_vvs";
            tv_name.setText(s[1]);
        }
        initPlayAv(avName);
    }



    @Override
    public void onResume() {
        super.onResume();
    }


    /**
     */
    private void initPlayAv(String avName) {
        mSurfaceView.reset();
        mSurfaceView.setLooping(true);
        mSurfaceView.setData("android.resource://"+getActivity().getPackageName()+"/raw/"+avName,MaterialVideoView.VIDEO_TYPE_FILE_PATH);
        mSurfaceView.start();
    }


    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSurfaceView .reset();
        mSurfaceView = null;
    }
}
